package net.xumf.web.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;


@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactoryMaster",//实体管理引用
        transactionManagerRef = "transactionManagerMaster",//失误管理引用
        basePackages = {"net.xumf.domain.master", "net.xumf.dao.master", "net.xumf.support.common", "net.xumf.service.master"}) //设置用户数据源所应用到的包
public class MasterDataSourceConfig {

    //注入用户数据源
    @Autowired
    @Qualifier("masterDataSource")
    private DataSource masterDataSource;

    @Autowired(required = false)
    private PersistenceUnitManager persistenceUnitManager;

    @ConfigurationProperties("spring.jpa")
    public JpaProperties masterJpaProperties() {
        return new JpaProperties();
    }

    @Bean
    public EntityManagerFactoryBuilder masterEntityManagerFactoryBuilder() {
        AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        Map<String, String> properties = masterJpaProperties().getProperties();
        properties.put("hibernate.naming.physical-strategy", "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
        return new EntityManagerFactoryBuilder(adapter,
                properties, this.persistenceUnitManager);
    }

    //配置EntityManager实体
    @Primary
    @Bean(name = "entityManagerMaster")
    public EntityManager entityManager(@Qualifier("masterEntityManagerFactoryBuilder") EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryMaster(builder).getObject().createEntityManager();
    }

    //配置EntityManager工厂实体
    @Primary
    @Bean(name = "entityManagerFactoryMaster")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryMaster(@Qualifier("masterEntityManagerFactoryBuilder") EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(masterDataSource)
                .packages("net.xumf.domain.master", "net.xumf.dao.master", "net.xumf.support.common", "net.xumf.service.master") //设置应用creditDataSource的基础包名
                .persistenceUnit("masterPersistenceUnit")
                .build();
    }

    //配置事务
    @Bean(name = "transactionManagerMaster")
    @Primary
    public PlatformTransactionManager transactionManagerMaster(@Qualifier("masterEntityManagerFactoryBuilder") EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryMaster(builder).getObject());
    }
}
